//线上地址
// let baseUrl = 'https://qyzrespack.pccedu.cn/ph'
//本地测试
let baseUrl = 'http://127.0.0.1:3000/tp5mall/public/index.php'
// method要大写，例如GET
// url,method,data,header,timeout
let http = function(p){
	let {url,method,data,header,timeout} = p
	// jwt
	header = {}
	if(uni.getStorageSync('token')){
		header.Authorization='Bearer '+uni.getStorageSync('token')
	}
	if(method=='POST'){
		header['content-type'] = 'application/x-www-form-urlencoded'
	}
	// uniapp的请求      
	return new Promise((resolve,reject)=>{
		uni.request({
			url: baseUrl+url,
			method,
			header,
			timeout,
			data,
			success: function(res) {
				console.log(res,'res')
				if(res.data.code==200||res.data.code==102){
					resolve(res)
				}else if(res.statusCode==401){
					// 用户身份校验失败
					uni.removeStorageSync('token')
					uni.reLaunch({
						url:'/pages/login'
					})
				}else{
					reject(res)
					uni.showToast({
						title:'请求失败',
						icon:'error'
					})
				}
			},
			fail: function(err) {
				uni.showModal({
					title: '提示',
					showCancel: false,
					content: '请求超时，请检查您的网络退出重新进入！',
						success: function (res) {
							if (res.confirm) {
								wx.exitMiniProgram()
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
				})
			},
			complete:function(){
				// console.log()
			}
		});
	})
	
}

export default {
	http
}